<template>
  <view class="warp">
    <view class="h20"></view>
    <view class="top">营业执照</view>
    <view class="update border" v-if="!img" @click="update">
      <image :src="this.$store.state.imgBaseUrl + '/id.png'" class="none-img"></image>
      <view>上传营业执照</view>  
    </view>
    <view class="update" v-else>
      <view class="delect" @click.stop="clearImg">x</view>
      <image :src="img" class="border-radius"></image>
    </view>
    <div class="text1" v-if="!isInfo">注：拍摄时请确保证件<text>边框完整，字体清晰，亮度均匀</text></div>
    
    <block v-if="isInfo">
      <view class="text2">
        请确认您的公司信息，若有误请点击修改
      </view>
      <view class="box">
        <view class="name">名称</view>
        <view class="inpt">
          <input type="text" v-model="info.companyName" @blur="backFixationTop()">
        </view>
      </view>
      <view class="box">
        <view class="name">法定代表人</view>
        <view class="inpt">
          <input type="text" v-model="info.name" @blur="backFixationTop()">
        </view>
      </view>
      <view class="box">
        <view class="name">注册地址</view>
        <view class="inpt">
          <input type="text" v-model="info.address" @blur="backFixationTop()">
        </view>
      </view>
      <view class="box">
        <view class="name"> 统一社会信用代码</view>
        <view class="inpt">
          <input type="text" v-model="info.regNum" @blur="backFixationTop()">
        </view>
      </view>
    </block>
    
    <view class="footer">
      <button type="primary" class="sub-btn cur" plain="true" @click="gotoPage">确认保存</button>
    </view>
    
  </view>
</template>

<script>
import {uploadFile} from "@/unit/common.js"
import formValid from '@/unit/graceChecker.js'
import {discernCharter, findV2ByUserId, personalQualification } from "@/service/certificationReplenish/certificationReplenish.service.js"
import { scrollToTargetPosition } from '@/mixin/index.mixin.js'
export default {
  mixins: [scrollToTargetPosition],
  data() {
    return {
      img: null,
      type: null, // 加盟的类型 person=>个人申请  enterprise=>企业申请
      allInfo: '',
      imgId: null,
      isInfo: false,
      isOver: '未完善',
      info: {
        companyName: '',
        name: '',
        address: '',
        regNum: ''
      },
      status: null
    };
  },
  onLoad(option) {
    this.status = option.status;
  },
  mounted() {
    this.initDraw()
  },
  activated () {
    this.initDraw()
  },
  methods: {
    initDraw () {
      // 判断是否已提交信息
      findV2ByUserId({}, res => {
        if (res.status == 0) {
          if (res.data) {
            this.allInfo = res.data;
            if (res.data.additionalStatus != null && res.data.additionalStatus != 2 && !this.status) {
              uni.showToast({
                title: '信息已提交！',
                icon: 'none',
                duration: 2000,
                success: () => {
                  setTimeout(() => {
                    uni.navigateTo({
                      url: '/pages/certificationReplenish/result'
                    })
                  }, 2000);
                }
              });
            }
            if (this.status) { // 驳回获取修改信息
              this.isInfo = true;
              this.img = res.data.businessLicenseFileId[0].url;
              this.imgId = res.data.businessLicenseFileId[0].id;
              this.info.companyName = res.data.storeName;
              this.info.name = res.data.realname;
              this.info.address = res.data.businessAddress;
              this.info.regNum = res.data.licenseNumber || '';
            }
          } 
        }
      })
    },
    update () {
      uploadFile({success: (obj) => {
        if (obj.status == 0) {
          this.getInfo(obj.data.url, obj.data.id)
        } else {
          uni.showToast({
            title: res.msg,
            icon: 'none'
          });
        }
      }})
    },
    getInfo (url, id) {
      let params = {
        id: id,
        appliedType: 2,
        isFace: 0,
        url: url
      }
      discernCharter(params, (res) => {
        if (res.status == 0) {
          this.info.companyName = res.data.name;
          this.info.name = res.data.person;
          this.info.address = res.data.address;
          this.info.regNum = res.data.regNum;
          this.isInfo = true;
          this.img = url;
          this.imgId = id;
        } else {
          this.isInfo = false;
          this.img = '';
          this.imgId = '';
          this.info.companyName = '';
          this.info.name = '';
          this.info.address = '';
          this.info.regNum = '';
          uni.showToast({
            title: res.msg,
            icon: 'none'
          });
        }
      })
    },
    clearImg () {
      this.img = null;
      this.isInfo = false;
      this.imgId = '';
      this.info.companyName = '';
      this.info.name = '';
      this.info.address = '';
      this.info.regNum = '';
    },
    gotoPage () {
      if (!this.isInfo) {
        uni.showToast({
          title: '请上传营业执照',
          icon: 'none'
        });
        return false;
      }
      let rule = [
        {name:'companyName', checkType: 'notnull', checkRule:'', errorMsg:'请完善营业执照信息'},
        {name:'name', checkType: 'notnull', checkRule:'', errorMsg:'请完善营业执照信息'},
        {name:'address', checkType: 'notnull', checkRule:'', errorMsg:'请完善营业执照信息'},
        {name:'regNum', checkType: 'notnull', checkRule:'', errorMsg:'请完善营业执照信息'},
      ];
      var checkRes = formValid.check(this.info, rule);
      if (!checkRes) {
        uni.showToast({ title: formValid.error, icon: "none" });
        return false;
      }
      // if (this.allInfo.realname != this.info.name) {
      //   uni.showToast({
      //     title: '营业执照法人姓名与身份证姓名不一致',
      //     icon: 'none'
      //   });
      //   return false;
      // }
      
      let params = {
        id: this.allInfo.id, // 资质id
        storeName: this.info.companyName,
        businessLicenseFileId: this.imgId,
        businessAddress: this.info.address,
        realname: this.info.name,
        licenseNumber: this.info.regNum
      }
      personalQualification(params, res => {
        if (res.status == 0) {
          uni.showToast({
            title: '提交成功',
            icon: 'none'
          });
          let licenseKey = uni.getStorageSync('licenseKey');
          if (licenseKey) {
            uni.removeStorageSync('licenseKey');
            this.$store.dispatch('getUserInfo'); // 更新userInfo信息
          }
          uni.redirectTo({ // 企业申请 => 上传凭证
            url: '/pages/certificationReplenish/result'
          })
        } else {
          uni.showToast({
            title: res.msg,
            icon: 'none'
          });
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
image {
  width: 100%;
  height: 100%;
}
.h20 {
  height: 20rpx;
  background-color: #FAFAFA;
}
.warp {
  height: calc(100vh - 20rpx);
  background-color: #FFFFFF;
  color: #333333;
  padding: 0 33rpx;
  .top {
    font-size: 36rpx;
    padding-top: 42rpx;
  }
  .update {
    width: 300rpx;
    height: 400rpx;
    border-radius: 16rpx;
    font-size: 24rpx;
    color: #666666;
    text-align: center;
    margin: 31rpx auto 0;
    position: relative;
    .none-img {
      display: block;
      width: 79rpx;
      height: 79rpx;
      margin: 124rpx auto 22rpx;
    }
    .border-radius {
      border-radius: 16rpx;
    }
    &.border {
      border: 1px dashed rgba(225,225,225,1);
    }
    .delect {
      width: 32rpx;
      height: 32rpx;
      line-height: 26rpx;
      color: #FFFFFF;
      text-align: center;
      background: rgba(51,51,51,1);
      opacity: 0.6;
      border-radius: 50%;
      z-index: 10;
      position: absolute;
      right: -38rpx;
      top: -35rpx;
    }
  }
  .text1 {
    text-align: center;
    font-size: 22rpx;
    margin-top: 33rpx;
    text {
      color: #C2AC78;
    }
  }
  
  .text2 {
    color: #333333;
    font-size: 22rpx;
    margin-top: 33rpx;
  }
  .box {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    padding: 30rpx 0;
    font-size: 28rpx;
    border-bottom: 1rpx solid #E1E1E1;
    .name {
      width: 145rpx;
      font-size: 28rpx;
    }
   
    .inpt {
      width: 73%;
      margin-left: 40rpx;
      display: flex;
      justify-content: space-between;
      .uni-list-cell {
        width: 100%;
        font-size: 28rpx;
      }
      .arrow {
        width: 11rpx;
        width: 21rpx;
        background-color: #999999;
      }
      input{
        width: 100%;
        font-size: 24rpx;
        font-weight:500;
        color: #999999;
      }
    }
    .label {
      font-size: 28rpx;
      color: #333333;
      font-weight:500;
    }
    .line {
      width: 120rpx;
      font-size: 24rpx;
      text-align: right;
    }
  }
  .footer {
    width: 100%;
    border-top: 1px solid #E1E1E1;
    padding: 14rpx 0 20rpx;
    background-color: #FFFFFF;
    position: absolute;
    bottom: 0;
    left: 0;
    button {
      height: 70rpx;
      margin: 0 32rpx;
      background-color: #636363;
      line-height: 70rpx;
      color: #ADA697;
      border: none;
      font-size: 30rpx;
      &.cur {
        color: #F6DA99;
        background-color: #434343;
      }
    }
  }
}
</style>
